// pages/barbecue/index.js
 var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hidden: false,
    curNav: 1,
    curIndex: 0,
    cart: [],
    cartTotal: 0,
    navList: [
      {
        id: 1,
        name: '理论考试',
        method:'findList'
      },
      {
        id: 3,
        name: '实操考试',
        method: 'handsOn'
      },
      {
        id: 2,
        name: '已报名',
        method: 'signuped'
      },
      {
        id: 4,
        name: '考试记录',
        method: 'examinateRecord'
      }
    ],
    dishesList: [],
    dishes: [],
    height:0,
    shenhe: false,
    audFalg: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this=this;
    //获取全局的标志，用于控制显示
    var audFlag = app.globalData.audFlag;
    var audSecFlag = app.globalData.audSecFlag;
    debugger
    debugger
    var dishesList = _this.data.dishesList;
    var arimg=[];


    if(audSecFlag==false){
      arimg[0] ="https://bjhylfkj.oss-cn-beijing.aliyuncs.com/web/fan.jpg";
      arimg[1] ="https://bjhylfkj.oss-cn-beijing.aliyuncs.com/web/pan.jpg";
      arimg[2] ="https://bjhylfkj.oss-cn-beijing.aliyuncs.com/web/empty.jpg";
      arimg[3] ="https://bjhylfkj.oss-cn-beijing.aliyuncs.com/knowing/userfiles/fileupload/202004/1254420221593853954.jpg";
      arimg[4] ="https://bjhylfkj.oss-cn-beijing.aliyuncs.com/knowing/userfiles/fileupload/202004/1254420487395287042.jpg";
      arimg[5] ="https://bjhylfkj.oss-cn-beijing.aliyuncs.com/knowing/userfiles/fileupload/202004/1254420698314252290.jpg";

    }else{
      arimg[0] ="https://bjhylfkj.oss-cn-beijing.aliyuncs.com/knowing/userfiles/fileupload/202004/1254418697069207554.jpg";
      arimg[1] ="https://bjhylfkj.oss-cn-beijing.aliyuncs.com/knowing/userfiles/fileupload/202004/1254419243637350402.jpg";
      arimg[2] ="https://bjhylfkj.oss-cn-beijing.aliyuncs.com/knowing/userfiles/fileupload/202004/1254419456783491074.jpg";
      arimg[3] ="https://bjhylfkj.oss-cn-beijing.aliyuncs.com/knowing/userfiles/fileupload/202004/1254420221593853954.jpg";
      arimg[4] ="https://bjhylfkj.oss-cn-beijing.aliyuncs.com/knowing/userfiles/fileupload/202004/1254420487395287042.jpg";
      arimg[5] ="https://bjhylfkj.oss-cn-beijing.aliyuncs.com/knowing/userfiles/fileupload/202004/1254420698314252290.jpg";
    }

    dishesList[1] = arimg;
     _this.setData({
      shenhe: audFlag,
      dishesList: dishesList,
       audFalg: audSecFlag,
    })

    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          height: res.windowHeight
        })
      }
    })
   

    this.loadingChange()

    app.paiRequest("/app/examinate/findList", { parentCode: 0}).then(function (res) {
      
      dishesList[0] = res.data;      
      _this.setData({
        dishesList: dishesList
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // var _this = this;
    // app.paiRequest("/app/extheory/examinate/findExtheory", { parentCode: 0 }).then(function (res) {
    //   var dishesList = _this.data.dishesList;
    //   dishesList[0] = res.data;
    //   _this.setData({
    //     dishesList: dishesList
    //   })
    // })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    var _this = this;
    let id = this.data.curNav, 
    index = parseInt(this.data.curIndex), 
    method = this.data.navList[index].method;

    self = this;
    this.setData({
      curNav: id,
      curIndex: index
    })
    var dishesList = this.data.dishesList;
    app.paiRequest("/app/examinate/" + method, { signup: id, parentCode: 0 }).then(function (res) {
      console.log(res);
      dishesList[index] = res.data;
      self.setData({
        dishesList: dishesList
      })
    })
    wx.hideNavigationBarLoading();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  loadingChange: function () {
    setTimeout(() => {
      this.setData({
        hidden: true
      })
    }, 2000)
  },
  selectNav: function (event) {
    let id = event.target.dataset.id, index = parseInt(event.target.dataset.index), method=event.target.dataset.method;

    self = this;
    this.setData({
      curNav: id,
      curIndex: index
    })
    var dishesList = this.data.dishesList;
    app.paiRequest("app/examinate/" + method, { signup:id, parentCode: 0 }).then(function (res) {
       console.log(res);
      dishesList[index] = res.data;
      self.setData({
        dishesList: dishesList
      })
    })
  },
  setStatus(dishId) {
    let dishes = this.data.dishesList;
    for (let dish of dishes) {
      dish.forEach((item) => {
        if (item.id == dishId) {
          item.status = !item.status || false
        }
      })
    }

    this.setData({
      dishesList: this.data.dishesList
    })
  },
  selectTap:function(e){
    console.log(e);
    var dataset=e.currentTarget.dataset;
    var src = dataset.src;
    var obj=JSON.stringify(dataset);
    wx.navigateTo({
      url: src + '?dataset=' + obj,
    })
  },
  detailpage:function(e){
    var dataset = e.currentTarget.dataset;
    var src = dataset.src;
    var obj = JSON.stringify(dataset);
    wx.navigateTo({
      url: src + '?dataset=' + obj,
    })
  },
  examvip:function(e){
    console.log(e);
    var dataset = e.currentTarget.dataset;
    var src = dataset.src;
    var obj = JSON.stringify(dataset);
    wx.navigateTo({
      url: src + '?dataset=' + obj,
    })
  },
  downloadfile: function (e) {
    console.log(e);
    var id = e.currentTarget.dataset.id;
    app.paiRequest("/app/examinate/ticket/download", { id: id }).then(function (res) {
      console.log(res)
      var url=res.data;
      wx.showLoading({
        title: '开始打开文件',
        icon: 'loading',
      })
      wx.downloadFile({
        url: app.data.service_url + url,
        success(res) {
          wx.saveFile({
            tempFilePath: res.tempFilePath,
            success: function (ress) {
              const saveFilePath = ress.savedFilePath;
              wx.openDocument({
                filePath: saveFilePath,
                success: function (ress) {
                  wx.showToast({
                    title: ress.errMsg,
                    icon: 'none',
                    duration: 3000,
                    mask: false
                  })
                }
              })
            },
            fail: function (err) {
              console.log('保存失败:' + err);
            }
          })
        },
        fail: function (res) {
          wx.showToast({
            title: "文件地不存在",
            icon: 'none',
            duration: 3000,
            mask: false
          })
        }
      })
      wx.hideLoading();
    })    
  },
})